package zhengyanze.com.aipoemmax.guide.guide;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.Constant;
import zhengyanze.com.aipoemmax.guide.utils.TypefaceUtil;

/**
 * 引导页碎片
 * Created by MrZheng on 2017/7/20.
 */

public class GuideFragment extends BasLazyeFragment {

    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.tv_zi1)
    TextView tvZi1;
    @BindView(R.id.tv_zi2)
    TextView tvZi2;
    @BindView(R.id.tv_poe2)
    TextView tvPoe2;
    @BindView(R.id.tv_poe1)
    TextView tvPoe1;
    @BindView(R.id.tv_yizhong)
    TextView tvYizhong;
    Unbinder unbinder;

    ArrayList<View> views = new ArrayList<>();
    int index = 0;

    String[] title;
    String[] content;
    int imageId;

    public void setText(String[] title,String[] content,int imageId) {
        this.title = title;
        this.content = content;
        this.imageId = imageId;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guide, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvZi1.setText(title[0]);
        tvZi2.setText(title[1]);

        TypefaceUtil typeUtil = new TypefaceUtil(getContext(), Constant.TTF.TTF_MAIN_TYPE);
        typeUtil.setTypeface(tvPoe1, false);
        tvPoe1.setText(content[0]);
        typeUtil.setTypeface(tvPoe2, false);
        tvPoe2.setText(content[1]);
        ivImage.setImageResource(imageId);

        views.add(tvYizhong);
        views.add(tvZi1);
        views.add(tvZi2);
        views.add(tvPoe1);
        views.add(tvPoe2);
        for (View v :
                views) {
            v.setAlpha(0);
        }
    }

    @Override
    public void showAnimaort() {
        startAnimort(views.get(index),1200);
    }

    public void startAnimort(View view,long time) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", new float[]{0f, 1f});
        animator.setDuration(time);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                index++;
                if (index < views.size()) {
                    if (index == 1 || index == 2) {
                        startAnimort(views.get(index),600);
                    }else{
                        startAnimort(views.get(index),1200);
                    }
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
